import { resolve } from "path";
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueJsx from '@vitejs/plugin-vue-jsx'
import progress from 'vite-plugin-progress'
import EslintPlugin from 'vite-plugin-eslint'
import { viteMockServe } from 'vite-plugin-mock'
import { ViteEjsPlugin } from 'vite-plugin-ejs'
import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"

//import WindiCSS from 'vite-plugin-windicss'

const root = process.cwd()
function pathResolve(dir) {
  return resolve(root, '.', dir)
}
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  let env = {}
  const isBuild = command === 'build'
  env = loadEnv(mode, root)

  return {
    plugins: [
      vue(),
      ViteEjsPlugin({
        title: env.VITE_APP_TITLE

      }),
      VueJsx(),
      progress(),
      // EslintPlugin({
      //   cache: false,
      //   include: ['src/**/*.vue', 'src/**/*.js', 'src/**/*.jsx'] // 检查的文件
      // }),
      VueI18nPlugin({
        runtimeOnly: true,
        compositionOnly: true,
        include: [resolve(__dirname, 'src/locales/lang/**')]
      }),
      viteMockServe({
        ignore: /^\_/,
        mockPath: 'mock',//解析根目录下的mock文件夹
        localEnabled: !env.VITE_USE_MOCK_ENV,  // 开发打包开关, //在开发环境中启用 mock
        prodEnabled: env.VITE_USE_MOCK_PRO,  // 生产打包开关  //在生产环境中启用 mock      
        injectCode: `
          import { setupProdMockServer } from '../mock/_createProductionServer'

          setupProdMockServer()
          `
      }),

      // createHtmlPlugin({
      //   minify: true,
      //   entry: '/src/main.js',
      //   inject: {
      //     // Inject data into ejs template
      //     data: {
      //       VUE_APP_TITLE: env.VITE_APP_TITLE,
      //     },
      //     // Embed the generated app.config.js file   
      //   },
      // }),
      //WindiCSS(),    
    ],
    resolve: {
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
      // 配置路径别名
      alias: [{
        find: 'vue-i18n',
        replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
      },
      {
        find: /\@\//,
        replacement: `${pathResolve('src')}/`
      }]
      // {
      //     '@': resolve(__dirname, 'src'), // 设置 `@` 指向 `src` 目录     
      //     'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      //   }, 

    },
    base: '/',// 设置打包路径
    server:
    {
      //allowedHosts: 'all',		
      open: true,//自动打开浏览器
      port: env.VITE_APP_PORT, //挂载端口
      //设置代理，根据我们项目实际情况配置
      proxy: {
        '/api': {
          target: env.VITE_APP_API_BASEURL,
          ws: true,
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/api/, '/')
        }
      }
    },
    build: {
      //target: 'es2015',
      // sourcemap: true
    },
  }
})